﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace DevSky.WebParts
{
    public class Tools
    {
        public static void Show(string message, bool closeModelDialog)
        {
            string cleanMessage = EscapeJSMssages(message);
            string script = string.Empty;
            if (closeModelDialog)
                script = "<script type=\"text/javascript\">alert(" + EscapeJSMssages(message) + ");window.frameElement.commitPopup();</script>";
            else
                script = "<script type=\"text/javascript\">alert(" + EscapeJSMssages(message) + ");</script>";
            Page page = HttpContext.Current.CurrentHandler as Page;

            if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
            {
                page.ClientScript.RegisterStartupScript(typeof(Tools), "alert", script);
            }
        }

        //http://stackoverflow.com/questions/806944/escape-quote-in-c-sharp-for-javascript-consumption
        public static string EscapeJSMssages(string message)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("\"");
            foreach (char c in message)
            {
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\"");
                        break;
                    case '\\':
                        sb.Append("\\\\");
                        break;
                    case '\b':
                        sb.Append("\\b");
                        break;
                    case '\f':
                        sb.Append("\\f");
                        break;
                    case '\n':
                        sb.Append("\\n");
                        break;
                    case '\r':
                        sb.Append("\\r");
                        break;
                    case '\t':
                        sb.Append("\\t");
                        break;
                    default:
                        int i = (int)c;
                        if (i < 32 || i > 127)
                        {
                            sb.AppendFormat("\\u{0:X04}", i);
                        }
                        else
                        {
                            sb.Append(c);
                        }
                        break;
                }
            }
            sb.Append("\"");

            return sb.ToString();

        }

        public static void CloseDialog()
        {
            if (HttpContext.Current.Request["IsDlg"] == "1")
            {
                HttpContext.Current.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
        }
    }
}